package unibratec.projeto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import unibratec.projeto.model.Evento;
import unibratec.projeto.model.EventoDAO;
import unibratec.projeto.model.Parada;
import unibratec.projeto.model.ParadaDAO;
import unibratec.projeto.util.AgendadorReceiver;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AbsSpinner;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class TelaCadastroEventoActivity extends Activity {

	private EditText edtDesc;
	private EditText edtData;
	private Spinner spnPonto;
	private Evento evento;
	private EventoDAO evDAO;
	private ParadaDAO parDAO;
	private List<Parada> paradas;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cadastrar_evento);
		
		edtDesc = (EditText) findViewById(R.id.editText1);
		edtData = (EditText) findViewById(R.id.editText2);
		spnPonto = (Spinner) findViewById(R.id.spinner1);
		
		if (getIntent().getSerializableExtra("eventoSelecionado") != null){
			evento = (Evento)getIntent().getSerializableExtra("eventoSelecionado");
			
			edtDesc.setText(evento.getDescricao());
		}
		
		parDAO = new ParadaDAO(this);
		paradas = parDAO.listar();
		ArrayAdapter<Parada> adapter = new ArrayAdapter<Parada>(this, android.R.layout.simple_spinner_item, paradas);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
		spnPonto.setAdapter(adapter);
		
	}
	
	public void Cadastrar(View v){
		Log.d("Igor", "Cadastrar!!");
		EventoDAO evDAO = new EventoDAO(this);
		
		if (evento == null){
			Evento e = new Evento(0, edtDesc.getText().toString());
		evDAO.inserir(e);
		Toast.makeText(this, "Cadastrado", Toast.LENGTH_SHORT).show();
		this.agendarEvento(e);
		finish();
		
		}else{
			evento.setDescricao((edtDesc.getText().toString()));
					
			evDAO.atualizar(evento);
			this.agendarEvento(evento);
			finish();
		}
	}
	
	public void atualizarAgenda(){
		Log.d("Igor", "atualizar!!");
		List<Evento> eventos = (List<Evento>) evDAO.listaAtualizada();
		
    	for (Evento evento : eventos) {
			this.agendarEvento(evento);
    	}
	}
	
	public void agendarEvento(Evento evento){
		Log.d("Igor", "agendar!!");
		AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
		Intent it = new Intent(this, AgendadorReceiver.class);
		//it.putExtra("evento", evento);
    	PendingIntent pi = PendingIntent.getBroadcast(this, 0, it, 0);
    	long time = System.currentTimeMillis() + (evento.getId_Evento() * 1000);
    	alarm.set(AlarmManager.RTC_WAKEUP, time, pi);
	}
	
	public void Voltar(View v){
		Log.d("Igor", "voltar!!");
		Intent it = new Intent(this, ListaEventosActivity.class);
		startActivity(it);
	}
	
}
